
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# '''更新邮件部分'''
# 第三方 SMTP 服务
class Mail(object):
    def __init__(self):
        self.mail_host = "smtp.qq.com"  # 设置服务器
        self.mail_user = "351244716@qq.com"  # 用户名
        self.mail_pass = "*********"  # 口令

    def set_basedata(self,sender='351244716@qq.com',receivers=['351244716@stu.hbeu.edu.cn']):
        self.sender = sender
        self.receivers = receivers  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱

    def set_msg(self,msg,from_='小王的服务器',to_='小王的邮箱',subject='新的信息来咯'):
        self.message = MIMEText(msg, 'plain', 'utf-8')
        self.message['From'] = Header(from_, 'utf-8')
        self.message['To'] = Header(to_, 'utf-8')
        self.message['Subject'] = Header(subject, 'utf-8')
    def set_msg_html(self,file,from_='小王的服务器',to_='小王的邮箱',subject='新的信息来咯'):
        f=open(file,'rb')
        msg=f.read()
        f.close()
        self.message = MIMEText(msg,_subtype='html',_charset='uft-8')
        self.message['From'] = Header(from_, 'utf-8')
        self.message['To'] = Header(to_, 'utf-8')
        self.message['Subject'] = Header(subject, 'utf-8')
    def send(self):
        try:
            smtpObj = smtplib.SMTP()
            smtpObj.connect(self.mail_host, 25)  # 25 为 SMTP 端口号
            smtpObj.login(self.mail_user, self.mail_pass)
            smtpObj.sendmail(self.sender, self.receivers, self.message.as_string())
            print("邮件发送成功")
        except smtplib.SMTPException as e:
            print("Error: 无法发送邮件.Case:%s" % e)
